//
//  UIImage+EXtension.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/10.
//  Copyright © 2018 itcast. All rights reserved.
//

#import "UIImage+EXtension.h"

@implementation UIImage (EXtension)

- (UIImage *)originalImage
{
    return [self imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

+ (UIImage *)originalImageWithImageName:(NSString *)imageName
{
    
    return [[self imageNamed:imageName] originalImage];
}

- (UIImage *)stretchableImage
{
    return [self stretchableImageWithLeftCapWidth:self.size.width / 2 topCapHeight:self.size.height / 2];
}

+ (UIImage *)stretchableImageWithImageName:(NSString *)imageName
{
    return [[self imageNamed:imageName] stretchableImage];
}

- (void)scaleToWidth:(CGFloat)width completion:(void (^)(UIImage * _Nonnull))completion
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        CGSize size = CGSizeMake(width, width / self.size.width * self.size.height);
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        completion(image);
    });
}


@end
